package sabokan.game.entities.levels.impl;

import java.util.List;
import java.util.Random;
import sabokan.game.entities.boxes.Box;
import sabokan.game.entities.characters.Char;
import sabokan.game.entities.items.Item;
import sabokan.game.entities.items.impl.imovables.WallShort;
import sabokan.game.entities.levels.Level;
import sabokan.game.entities.players.Player;
import sabokan.game.entities.players.impl.PlayerBoy;
import sabokan.game.entities.tiles.Tile;
import sabokan.mazes.Maze;
import sabokan.mazes.MazeGenerator;

/**
 *
 * @author anaka
 */
public class AutoGeneratedLevel extends Level {
    private int size;
    
    @Override
    protected Tile[][] initializeLevel() {
        size = 30 + new Random().nextInt(7);
        Maze maze = new MazeGenerator().generateSaveMaze(size);
        return generateLevelFromMaze(maze);
    }

    @Override
    public List<Box> initBoxes() {
        return boxes;
    }

    @Override
    public List<Char> initCharacters() {
        return characters;
    }

    @Override
    public List<Item> initItems() {
        return items;
    }

    @Override
    public Player initPlayer() {
        return player;
    }

    private Tile[][] generateLevelFromMaze(Maze maze) {
        Tile[][] generated = new Tile[size][size];
        
        for (int x = 0; x < size; x++) {
            for (int y = 0; y < size; y++) {
                if (maze.isEnd(x, y)) {
                    generated[x][y] = Tile.EXIT;
                } else if (maze.isStart(x, y)) {
                    generated[x][y] = Tile.DIRT;
                    player = new PlayerBoy(x, y);
                } else if (maze.isWall(x, y)) {
                    generated[x][y] = Tile.WATER;
                    items.add(new WallShort(x, y));
                } else {
                    generated[x][y] = Tile.GRASS;
                }
            }
        }
        
        return generated;
    }
}
